package io.renren.common.utils;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;


public class DTOUtils {

    
	public static <S, T> List<T> mapList(List<S> source, Class<T> targetClass, String mapMethod, Class<S> sourceClass) {
		List<T> list = new ArrayList<>();
		if (source != null) {
			try {
				Method method = targetClass.getMethod(mapMethod, sourceClass);
				for (S s : source) {
					@SuppressWarnings("unchecked")
					T target = (T) method.invoke(null, s);
					list.add(target);
				}
			} catch (Exception e) {
				e.printStackTrace();
				//
			}
		}
        return  list;
	}
    public static <S, T> List<T> mapList(List<S> source, Class<T> targetClass) {
        String mapMethod="modelToDto";
        List<T> list = new ArrayList<>();
        if(source==null||source.size()==0){
            return list;
        }
        Class  sourceClass = source.get(0).getClass();
        if (source != null) {
            try {
                Method method = targetClass.getMethod(mapMethod, sourceClass);
                for (S s : source) {
                    @SuppressWarnings("unchecked")
                    T target = (T) method.invoke(null, s);
                    list.add(target);
                }
            } catch (Exception e) {
                e.printStackTrace();
                //
            }
        }
        return  list;
    }


    

    

   
}
